package com.wbsxch.algorithm01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

public class Input {

    public static String[] readFile(String file){
        StringBuilder sb = new StringBuilder();
        try (FileInputStream in = new FileInputStream(new File(file))) {
            byte[] bytes = new byte[1024];
            int len;
            while ((len = in.read(bytes)) > 0){
                sb.append(new String(bytes,0,len).replaceAll("\n", ",").replaceAll("\\s", ""));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString().split(",");
    }

    public static int[] readFileAndSort(String file){
        String[] strings = readFile(file);
        System.out.println("StringsLength:"+strings.length);
        int[] arr = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            arr[i] = Integer.parseInt(strings[i]);
        }
        Arrays.sort(arr);
        return arr;
    }
}
